﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using QRCode.BusinessLayer;
using QRCode.BusinessEntities;
using System.IO;
using QRCode.ViewModel;

namespace QRCode.Web.Controllers
{
    [UserAuthorize]
    public class ScanController : Controller
    {
        CodeManager codeManager = new CodeManager();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Export()
        {
            return View("Export");
        }
        [HttpPost]
        public JsonResult SaveQRCode(string emp)
        {
            Response _response = new Response();
            if (ModelState.IsValid)
            {
                Code _code = new Code();
                _code.CodeCont = emp;
                _code.CreatTime = DateTime.Now;
                //string abc = Session["UserID"].ToString();
                int uID = Session["UserID"] == null ? -1 : Convert.ToInt32(Session["UserID"]);
                if (uID < 0) return Json(_response);
                _code.UserID =uID;
                _response = codeManager.Add(_code);
                if (_response.Code == 1)
                {
                    _response.Message = "二维码：【" + emp + "】，成功记录。";
                }
            }
            else
            {
                _response.Code = 0;
                _response.Message = General.GetModelErrorString(ModelState);
            }
            return Json(_response);
        }
        [HttpPost]
        public JsonResult CodeListJson()
        {
            SelectInfoViewModel sInfo = new SelectInfoViewModel();
            if (Request.IsAjaxRequest())
            {
                var stream = HttpContext.Request.InputStream;
                string json = new StreamReader(stream).ReadToEnd();
                System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
                sInfo = js.Deserialize<SelectInfoViewModel>(json);
            }
            return Json(codeManager.FindList(Convert.ToInt32(Session["UserID"]),sInfo.StartDate,sInfo.EndDate.AddDays(1),sInfo.SearchIndex));
        }
        [HttpPost]
        public void ExportListJson()
        {
            //List<Code> list = new List<Code>();
            List<ExportCodeViewModel> vList = new List<ExportCodeViewModel>();
            if (Request.IsAjaxRequest())
            {
                var stream = HttpContext.Request.InputStream;
                string json = new StreamReader(stream).ReadToEnd();
                System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
                vList = js.Deserialize<List<ExportCodeViewModel>>(json);
            }
            List<int> cIds = new List<int>();
            foreach (var j in vList)
            {
                cIds.Add(j.CodeId);
            }
            IEnumerable<Code> codelist = codeManager.FindList(cIds);
            codelist.All(x => { x.ExportTime = DateTime.Now; return true; });
            codeManager.Update(codelist);
            //return Json(list);
        }
        [HttpPost]
        public JsonResult Delete(int id)
        {
            return Json(codeManager.Delete(id));
        }
        public PartialViewResult DateRange()
        {
            return PartialView("DateRange");
        }
    }
}